<div id="notification">
  <?php if (isset($success)) : ?>
    <?php if ($success) : ?>
    <div class="alert alert-success">
      <?= $msg ?>
    </div>
    <?php endif; ?>
  <?php endif; ?>
</div>
<form class="form-horizontal" action="<?= URL ?>" method="post">
  <input type="hidden" name="page" value="cai_dat" />
  <fieldset>
    <legend>Cài đặt</legend>
    <div class="control-group">
      <label class="control-label" for="sender">Người gửi</label>
      <div class="controls">
        <input name="config[sender]" value="<?= isset($g_config['sender'])? $g_config['sender'] : '' ?>" type="text" class="input-xlarge" id="sender" />
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="from">Mail người gửi</label>
      <div class="controls">
        <input name="config[from]" value="<?= isset($g_config['from'])? $g_config['from'] : '' ?>" type="text" class="input-xlarge" id="from">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="protocol">Giao thức gửi</label>
      <div class="controls">
        <select name="config[protocol]" id="protocol">
          <option <? if (isset($g_config['protocol']) AND $g_config['protocol'] == 'mail') echo 'selected="selected"' ?>>mail</option>
          <option <? if (isset($g_config['protocol']) AND $g_config['protocol'] == 'smtp') echo 'selected="selected"' ?>>smtp</option>
        </select>
      </div>
    </div>
    <fieldset id="smtp_fs">
      <legend>SMTP</legend>
      <div class="control-group">
        <label class="control-label" for="hostname">Máy chủ SMTP</label>
        <div class="controls">
          <input name="config[hostname]" value="<?= isset($g_config['hostname'])? $g_config['hostname'] : '' ?>" type="text" class="input-xlarge" id="hostname">
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="username">Tài khoản SMTP</label>
        <div class="controls">
          <input name="config[username]" value="<?= isset($g_config['username'])? $g_config['username'] : '' ?>"  type="text" class="input-xlarge" id="username">
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="password">Mật khẩu SMTP</label>
        <div class="controls">
          <input name="config[password]" value="<?= isset($g_config['password'])? $g_config['password'] : '' ?>"  type="text" class="input-xlarge" id="password">
        </div>
      </div>              
      <div class="control-group">
        <label class="control-label" for="port">Cổng SMTP</label>
        <div class="controls">
          <input name="config[port]" value="<?= isset($g_config['port'])? $g_config['port'] : '' ?>"  type="text" class="input-xlarge" id="port">
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="timeout">SMTP timeout</label>
        <div class="controls">
          <input name="config[timeout]" value="<?= isset($g_config['timeout'])? $g_config['timeout'] : '' ?>"  type="text" class="input-xlarge" id="timeout">
        </div>
      </div>
    </fieldset>
    <div class="form-actions">
      <button type="submit" name="act" value="update" class="btn btn-primary">Lưu lại</button>
    </div>
  </fieldset>
</form>
<script type="text/javascript">
  // disable smtp fieldset
  (function () {
    toggleActiveSmtp();

    $('#protocol').on('change', toggleActiveSmtp);
  })();

  function toggleActiveSmtp() {
    var $smtp_fs = $('#smtp_fs');
    if ( $('#protocol').val().toLowerCase() == 'mail' ) {

      // for chrome
      if ( typeof $smtp_fs.prop('disabled') == 'undefined' ) {
        $smtp_fs.find('input').prop('disabled', true);
      } 
      // for firefox
      else {
        $smtp_fs.prop('disabled', true);
      }
    } else { // smtp
      // for chrome
      if ( typeof $smtp_fs.prop('disabled') == 'undefined' ) {
      console.log($smtp_fs.find('input'));
        $smtp_fs.find('input').prop('disabled', false);
      } 
      // for firefox
      else {
        $smtp_fs.prop('disabled', false);
      }
    }
  }
</script>